package org.example.lethear.dto.common;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 分页结果DTO
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PageResult<T> {
    
    /**
     * 数据列表
     */
    private List<T> items;
    
    /**
     * 总记录数
     */
    private Long total;
    
    /**
     * 当前页码
     */
    private Integer page;
    
    /**
     * 每页大小
     */
    private Integer size;
    
    /**
     * 总页数
     */
    private Integer totalPages;
    
    /**
     * 是否有上一页
     */
    private Boolean hasPrevious;
    
    /**
     * 是否有下一页
     */
    private Boolean hasNext;
    
    /**
     * 是否是第一页
     */
    private Boolean isFirst;
    
    /**
     * 是否是最后一页
     */
    private Boolean isLast;
    
    /**
     * 创建分页结果
     */
    public static <T> PageResult<T> of(List<T> items, Long total, Integer page, Integer size) {
        int totalPages = (int) Math.ceil((double) total / size);
        
        PageResult<T> result = new PageResult<>();
        result.items = items;
        result.total = total;
        result.page = page;
        result.size = size;
        result.totalPages = totalPages;
        result.hasPrevious = page > 1;
        result.hasNext = page < totalPages;
        result.isFirst = page == 1;
        result.isLast = page == totalPages;
        return result;
    }
} 